package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmSensitiveServiceImpl implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult SensitiveSave(WmSensitive wmSensitive) {
        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String sensitives = wmSensitive.getSensitives();
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives,sensitives);
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        Integer count = wmSensitiveMapper.selectCount(wrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public PageResponseResult SensitiveList(SensitiveDto dto) {
        if (dto==null){
            return  new PageResponseResult();
        }
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(dto.getName()),WmSensitive::getSensitives,dto.getName());
        Integer size = dto.getSize();
        Integer page = dto.getPage();
        size=size==null?10:size;
        page=page==null?1:page;
        Page<WmSensitive> page1 = new Page<>(page,size);
        Page<WmSensitive> wmSensitivePage = wmSensitiveMapper.selectPage(page1, wrapper);
        Long total = wmSensitivePage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page,size,total.intValue());
        pageResponseResult.setData(wmSensitivePage.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult SensitiveUpdate(WmSensitive wmSensitive) {
        if (wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getId,wmSensitive.getId());
        int update = wmSensitiveMapper.update(wmSensitive, wrapper);
        return update>0?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult SensitiveDel(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getId,id);
        int delete = wmSensitiveMapper.delete(wrapper);
        return delete>0?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
